﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Data;
using System.Text;
using Tab2Sql.Classes.Core;

namespace Tab2Sql
{
    /// <summary>
    /// class intended to analyse text data to suggest primary keys, etc. 
    /// </summary>
    public class DBAnalyse
    {
        /// <summary>
        /// Row count
        /// </summary>
        public int TRows
        {
            get { return  myTable.Rows.Count; }
              }
        /// <summary>
        /// Column Count
        /// </summary>
        public int TColumns
        {
            get { return myTable.Columns.Count; }
        }

        private DataTable myTable;

        //TODO : Refactor/revisit/re-wtf.
        /// <summary>
        /// Outputs text about a datatable. DOES NOT WORK
        /// </summary>
        /// <returns></returns>
        public string AnalysisText()
        {
            StringBuilder endStr = new StringBuilder();
            int frequency = 0;
            string tmpBiggestValue = "";
            int tmpBiggest = 0;
            bool isUnique = false;
            foreach (var k2 in myList)
            {
                tmpBiggest = 0;
                isUnique = false;
                string kColumnName= k2.Key;
                Hashtable ht = k2.Value;
                endStr.AppendLine(kColumnName + "------------------------------------------------");
                foreach (object columnValue in ht.Keys)
                {
                    frequency = (int)ht[columnValue];
                    if (frequency > tmpBiggest)
                    {
                        tmpBiggest = frequency;
                        tmpBiggestValue = columnValue.ToString();
                    }



                    if (tmpBiggest == 1)
                    {
                        isUnique = true;
                    }
                    if (isUnique == true)
                    {
                        endStr.Append(" UNIQUE");
                    }
                    endStr.AppendLine();
                }
            }
            return endStr.ToString();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="externalTable">
        /// A <see cref="xTab2Sql"/>
        /// </param>
        public DBAnalyse(xTab2Sql externalTable)
        {
            if (externalTable.IsProcessed == false)
            {
                externalTable.Process();
            }
            myTable = externalTable.OurDataTable;
        }


        private Dictionary<string, Hashtable> myList
        {
            get
            {
                Dictionary<string, Hashtable> tmpList = new Dictionary<string, Hashtable>();
     
                foreach (DataColumn dc in myTable.Columns)
                {
                    Hashtable hTable = new Hashtable();
                    foreach (DataRow dr in myTable.Rows)
                    {
                            var tmpValue = dr[dc];
                        if (!hTable.ContainsKey(dr[dc]))
                        {
                            hTable.Add(tmpValue, 1);
                        }
                        else
                        {
                            hTable[tmpValue] = (int)hTable[tmpValue] + 1;
                        }
                    }
                    tmpList.Add(dc.ColumnName, hTable);
                }
                return tmpList;
            }
        }


    }
}

